WGQuery.prototype = Array.prototype;

function WGQuery(arg) {

    if (typeof arg === 'string') {
        getElements.call(this, arg);
    } else if (typeof arg ===  'object') {
        this.push(arg);
    }

}


// 入口函数
function $(arg) {
    return new WGQuery(arg);
}


// getElements
function getElements(arg) {
    var eles = document.querySelectorAll(arg);
    for (var i = 0; i < eles.length; i++) {
        this.push(eles[i]);
    }
}   


// 封装css方
WGQuery.prototype.css = function() {

    if (arguments.length == 1) {

        // 获取参数
        var arg = arguments[0];

        if (typeof arg === 'string') {
            return getComputedStyle(this[0])[arg];
        } else if (typeof arg === 'object') {

            this.forEach(function(ele, i) {

                for (var key in arg) {
                    if (typeof arg[key] === 'string') {
                        ele.style[key] = arg[key];

                    } else if(typeof arg[key] === 'function') {
                        ele.style[key] = arg[key](i);
                    }
                }

            });


        }

    } else if (arguments.length == 2) {
        var arg1 = arguments[0];
        var arg2 = arguments[1];


        this.forEach(function(ele, i) {

            if (typeof arg2 === 'string') {
                ele.style[arg1] = arg2;
            } else if (typeof arg2 === 'function') {
                ele.style[arg1] = arg2(i);
             }
         });

        

            
    }
    // 链式操作
    return this;


};



WGQuery.prototype.remove = function() {
    for (var i = this.length - 1; i >= 0; i--) {
        this[i].remove();
    }
    return this;
}; 

WGQuery.prototype.eq = function(index) {
    return $(this[index]);
} 


WGQuery.prototype.click = function(func) {

    if (typeof func === 'function') {

        this.forEach(function(ele) {

            ele.onclick = function(envent) {
                func.call(this, event);
            };
        });

    }

    return this;

};















